package io.renren.modules.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.seller.dao.OrderDao;
import io.renren.modules.seller.entity.OrderEntity;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.service.OrderService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>().eq("seller_id", userId)
        );

        return new PageUtils(page);
    }

    @Override
    public PageResult findPage(OrderEntity orderEntity, Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        Long orderId = orderEntity.getOrderId();
        String status = orderEntity.getStatus();
        String paymentType = orderEntity.getPaymentType();
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
                        .eq("seller_id", userId)
                        .eq(orderId != null, "order_id", orderId)
                        .eq(StringUtils.isNotBlank(status), "status", status)
                        .eq(StringUtils.isNotBlank(paymentType), "payment_type", paymentType)
        );

        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    public PageResult findPage(Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>().eq("seller_id", userId)
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

}
